package com.fenghongzhang.day002;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private ImageView img;
    private static final String TAG = "MainActivity";
    private Disposable disposable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img = (ImageView) findViewById(R.id.img);

        Retrofit build = new Retrofit.Builder()
                .client(new OkHttpClient.Builder()
                        .addInterceptor(new MyIntercepter())
                        .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).build())
                .baseUrl(ServerApi.BASE_URL)
                .addConverterFactory(MyGsonFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        ServerApi serverApi = build.create(ServerApi.class);
        Observable<Food> food = serverApi.getFood();
        food.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Food>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.i(TAG, "onSubscribe: ");
                disposable = d;
            }

            @Override
            public void onNext(Food value) {
                Log.i(TAG, "onNext: "+value.getData().get(0).getTitle());
//                Log.i(TAG, "onNext: "+value.getData().get(0).getDuo());
                for (int i = 0; i < value.getData().size(); i++) {
                    Food.DataBean dataBean = value.getData().get(i);
                    Log.i(TAG, "onNext: "+dataBean.getDuo());
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG, "onError: "+e.getMessage());
            }

            @Override
            public void onComplete() {
                Log.i(TAG, "onComplete: ");
            }
        });
    }

    private void test() {
        Glide.with(this).load("").into(img); //500
        ImageLoader.loadImgCircle(this,"",img);


//        SharedPreferences sp = getSharedPreferences("哈哈", MODE_PRIVATE);
//        SharedPreferences.Editor edit = sp.edit();
//        edit.pu
        SpUtils.getInstance().put("name","小明");
        SpUtils.getInstance().put("age",123);

        String name = SpUtils.getInstance().getSharedPreferences().getString("name", "123");
        Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        disposable.dispose();
    }
}
